//
//  CoinViewController.swift
//  Exam-iOS
//
//  Created by 李胜锋 on 2018/2/7.
//  Copyright © 2018年 李胜锋. All rights reserved.
//

import UIKit


class CoinViewController: TableViewController {

    
    @IBOutlet weak var coinLabel: UILabel!
    
    var recharge = Recharge()
    
    var complete: VoidBlock?
    
    var coins:Int = 0
    var coinList = [CoinModel]()
    
    static func controller() -> CoinViewController {
        let vc = CoinViewController.viewController(storyboardName: "Mine") as! CoinViewController
        return vc
    }
    
    @IBAction func backClick(_ sender: UIBarButtonItem) {
        navigationController?.popViewController(animated: true)
        self.complete?()
    }
    
                                               
    @IBAction func detailClick(_ sender: UIBarButtonItem) {
        if AccountStorage.default.isLogin()  == false {
            showLogin()
            return
        }
        
        let vc = PurchaseHistoryViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
 
    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
    }

    func loadData() {
        startAnimating()
        shurenProvider.rx.request(.productCoins).asObservable().mapRespnseToObjectArray(type: CoinModel.self).subscribe(onNext: { (coins) in
            self.coinList = coins
        }, onError: { (error) in
            stopAnimating()
            showError(error)
        }, onCompleted: {[weak self] in
            stopAnimating()
            self?.tableView.reloadData()
        }).disposed(by: disposeBag)
        
        
        shurenProvider.rx.request(.userInfo).asObservable().mapRespnseToObject(type: AccountModel.self).subscribe(onNext: { (account) in
            AccountStorage.default.updateAccount(account)
        }, onError: { (error) in
            showError(error)
        }, onCompleted: { [weak self] in
            self?.coinLabel.text = "\(AccountStorage.default.account.goldCoin ?? 0)"
        }).disposed(by: disposeBag)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return coinList.count
    }

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CoinCell", for: indexPath) as! CoinCell
        
        if indexPath.row < coinList.count {
            let item = coinList[indexPath.row]
            let title = item.name
            let price = item.price
            cell.set(title: title, price: price)
        }

        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        if AccountStorage.default.isLogin()  == false {
            showLogin()
            return
        }
        
        if indexPath.row < coinList.count {
            let item = coinList[indexPath.row]
            guard let productId = item.productId else { return }
            recharge.recharge(productId, complete: { [weak self] in
                self?.coinLabel.text = "\(AccountStorage.default.account.goldCoin ?? 0)"
//                lsf_alert("充值成功")
            })
        }
    }
}

